package weaver.jobs.sinopec.cron;

import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.sinopec.utils.BatchCreateModeTableUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年10月08日 09:30
 */
public class WeaBatchCreateModeTableCronJob extends BaseCronJob {
    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            baseBean.writeLog(">>>>>WeaBatchCreateModeTableCronJob>>>>>curdate=" + DateUtil.getCurrentDate());
            RecordSet rs = new RecordSet();
            User user = new User(1);
            BatchCreateModeTableUtil batchCreateModeTableUtil = new BatchCreateModeTableUtil();
            int count = 0;
            rs.executeQuery("select id,oldwfname,newwfformid,newtablename,newformid,newmodeid from uf_lcsjqyjlb where status=0");
            int counts = rs.getCounts();
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                String modeName = Util.null2String(rs.getString("oldwfname"));
                String maintablename = Util.null2String(rs.getString("newtablename"));
                String newwfformid = Util.null2String(rs.getString("newwfformid"));
                int newformid = Util.getIntValue(rs.getString("newformid"),-1);
                int newmodeid = Util.getIntValue(rs.getString("newmodeid"),-1);
                if(Math.abs(newformid)<=0 && Math.abs(newmodeid)<=0){
                    continue;
                }
                JSONObject mainTableInfo = new JSONObject();
                mainTableInfo.put("modeName", modeName + "历史迁移");
                mainTableInfo.put("maintablename", maintablename);
                Map<String, Object> mainResultMap = batchCreateModeTableUtil.createModeMainInfo(user, mainTableInfo, newwfformid);
                baseBean.writeLog(">>>>>创建建模主表完成后结果信息>>>>>mainResultMap=" + mainResultMap);
                Map<String, Object> resultModeInfo = (Map<String, Object>) mainResultMap.get("resultModeInfo");
                String newFormid = Util.null2String(resultModeInfo.get("formid"));
                String newModeid = Util.null2String(resultModeInfo.get("modeid"));
                List<Map<String, Object>> detailResultList = new ArrayList<>();
                List<String> detailtableList = batchCreateModeTableUtil.getDetailtable(newwfformid);
                int index = 1;
                for (String detailtablename : detailtableList) {
                    JSONObject detailTableInfo = new JSONObject();
                    detailTableInfo.put("newDetailtablename", maintablename + "_" + detailtablename.split("_")[3]);
                    detailTableInfo.put("newFormid", resultModeInfo.get("formid"));
                    Map<String, Object> detailResultMap = batchCreateModeTableUtil.createModeDeatilInfo(user, detailTableInfo, newwfformid, detailtablename);
                    detailResultList.add(detailResultMap);
                    index++;
                }
                baseBean.writeLog(">>>>>创建建模所有明细表结果信息>>>>>detailResultList=" + detailResultList);
                JSONObject detailTableLogInfo = new JSONObject();
                detailTableLogInfo.put("newDetailtablename", maintablename + "_dt" + index);
                detailTableLogInfo.put("newFormid", resultModeInfo.get("formid"));
                Map<String, Object> detaillogResultMap = batchCreateModeTableUtil.createRequestLogTable(user, detailTableLogInfo);
                baseBean.writeLog(">>>>>创建建模流程签字意见表完成后结果信息>>>>>detaillogResultMap=" + detaillogResultMap);
                String resultStr = "";
                if (Util.getIntValue(newFormid) > 0) {
                    resultStr = "建模表创建完成;";
                }
                if (Util.getIntValue(newModeid) > 0) {
                    resultStr = "建模模块创建完成;";
                }
                if (!"".equals(resultStr)) {
                    String updatesql = "update uf_lcsjqyjlb set newformid='" + newFormid + "',newmodeid='" + newModeid + "',result='" + resultStr + "' where id=" + id;
                    rs.executeUpdate(updatesql);
                    count++;
                }
            }
            baseBean.writeLog(">>>>>最终完成后所有结果信息>>>>>counts=" + counts + ",successTotal=" + count);
        } catch (Exception e) {
            baseBean.writeLog("WeaBatchCreateModeTableCronJob", e);
            e.printStackTrace();
        }

    }
}
